package net.mikoo.seals.api.model.apply;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.common.enums.ApplyStatusEnum;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.Apply;

public class ApplyRecordResponse extends BaseResponse {

	private static final long serialVersionUID = 1067351291090959186L;

	private List<MyApplyInfo> applys;

	public List<MyApplyInfo> getApplys() {
		return applys;
	}

	public void setApplys(List<MyApplyInfo> applys) {
		this.applys = applys;
	}
	
	public static ApplyRecordResponse valueOf(PageModel<Apply> pm) {
		ApplyRecordResponse result = new ApplyRecordResponse();
		if(pm == null || pm.getDatas() == null || pm.getDatas().isEmpty()) {
			return result;
		}
		
		List<MyApplyInfo> applys = new ArrayList<MyApplyInfo>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(Apply apply : pm.getDatas()) {
			
			if(apply == null || apply.getSeal() == null) {
				continue;
			}
			
			MyApplyInfo applyInfo = new MyApplyInfo();
			applyInfo.setApplyId(apply.getApplyId());
			applyInfo.setAlias(apply.getSeal().getSealName());
			
			if(apply.getApplyTime() != null) {
				applyInfo.setApplyTime(sdf.format(apply.getApplyTime()));
			}
			
			ApplyStatusEnum applyStatus = ApplyStatusEnum.getByCode(apply.getApplyStatus());
			if(applyStatus != null) {
				applyInfo.setStatus(applyStatus.getCode());
				applyInfo.setStatusName(applyStatus.getMessage());
			}
			applys.add(applyInfo);
		}
		result.setApplys(applys);
		return result;
	}
	
}
